package com.swpu.cbg.cnshop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.swpu.cbg.cnshop.Constans;
import com.swpu.cbg.cnshop.R;
import com.swpu.cbg.cnshop.activity.ShopDetailActivity;
import com.swpu.cbg.cnshop.adapter.DividerItemDecortion;
import com.swpu.cbg.cnshop.adapter.HomeAdapter;
import com.swpu.cbg.cnshop.bean.Banner;
import com.swpu.cbg.cnshop.bean.HomeCategory;
import com.swpu.cbg.cnshop.bean.HomeContentCategory;
import com.swpu.cbg.cnshop.http.BaseCallback;
import com.swpu.cbg.cnshop.http.OkHttpHelper;
import com.swpu.cbg.cnshop.http.SpotsCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenboge on 16/1/30.
 */
public class HomeFragment extends BaseFragment {

    private View mView;
    private LayoutInflater mInflater;
    private SliderLayout sliderShow;
    private PagerIndicator mIndicater;
    private RecyclerView mRecycleView;
    private List<HomeCategory> mDatas;
    private HomeAdapter mAdapter;
    private Gson mGson = new Gson();
    private List<Banner> mBanners;
    private OkHttpHelper mHelper;
    private Handler mHandler;


    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mInflater = LayoutInflater.from(getActivity());
        mView = mInflater.inflate(R.layout.home, container, false);
        return mView;
    }

    @Override
    public void init() {
        mHandler = new Handler(Looper.getMainLooper());
        mHelper = OkHttpHelper.getInstance();
        initSlidview();
        requestHeaderImageData();
        initRecycleView();
    }

    private void initRecycleView() {
        mRecycleView = (RecyclerView) mView.findViewById(R.id.id_home_recyclerview);
        mDatas = new ArrayList<>();


        mHelper.get(Constans.API.HOME_CONTENT__URL,null, new BaseCallback<List<HomeContentCategory>>() {

            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onResponse(Response response) {

            }

            @Override
            public void onSuccess(Response response, List<HomeContentCategory> homeContentCategories) {
                initrEcycleViewData(homeContentCategories);
            }


            @Override
            public void onError(Response response, int code, Exception e) {

            }

            @Override
            public void onTokenError(Response response, int code) {

            }
        });

    }

    private void initrEcycleViewData(List<HomeContentCategory> homeContentCategories) {
        mAdapter = new HomeAdapter(getActivity(), homeContentCategories);
        mAdapter.setOnItemClickListener(new HomeAdapter.onItemClickListener() {
            @Override
            public void onClick(View view, HomeContentCategory category) {
                Toast.makeText(getActivity(),category.getTitle(),Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(getActivity(), ShopDetailActivity.class);
                intent.putExtra(Constans.API.CAMPAIGN_ID, category.getId());
                startActivity(intent,false);
            }
        });

        mRecycleView.setAdapter(mAdapter);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //mRecycleView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecycleView.addItemDecoration(new DividerItemDecortion());

    }

    private void initSlidview() {
        sliderShow = (SliderLayout) mView.findViewById(R.id.slider);
        mIndicater = (PagerIndicator) mView.findViewById(R.id.custom_indicator);

        //initSliderViewData();
    }

    private void initSliderViewData() {

        if (mBanners != null) {
            for (Banner banner : mBanners) {

                TextSliderView textSliderView = new TextSliderView(getActivity());
                textSliderView
                        .description(banner.getName())
                        .image(banner.getImgUrl());
                //点击事件
                textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);
                textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                    @Override
                    public void onSliderClick(BaseSliderView slider) {


                    }
                });

                sliderShow.addSlider(textSliderView);
            }

        }

        //设置底部导航状态栏
        // sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        sliderShow.setCustomIndicator(mIndicater);

        sliderShow.setCustomAnimation(new DescriptionAnimation());
        sliderShow.setPresetTransformer(SliderLayout.Transformer.RotateUp);
        sliderShow.setDuration(3000);

        sliderShow.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public void requestHeaderImageData() {


        Map<String, Object> data = new HashMap<>();
        data.put("type", "1");

        mHelper.post(Constans.API.HOME_TOP_URL, data, new SpotsCallback<List<Banner>>(getActivity()) {

            @Override
            public void onSuccess(Response response, List<Banner> banners) {
                mBanners = banners;
                initSliderViewData();
            }

            @Override
            public void onError(Response response, int code, Exception e) {
                throw new RuntimeException("网络出现异常");
            }

            @Override
            public void onTokenError(Response response, int code) {

            }
        });

    }


    @Override
    public void onStop() {
        sliderShow.stopAutoCycle();
        super.onStop();

    }
}
